package rs.networx.servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

//import rs.networx.dao.NottificationCategory;
import rs.networx.dao.NottificationDao;
import rs.networx.dao.PersonDao;
//import rs.networx.dao.PostCategory;
import rs.networx.dao.PostDao;
import rs.networx.entity.Nottification;
import rs.networx.entity.Person;
import rs.networx.entity.Post;

public class AddVideoServlet extends HttpServlet {

	private static final long serialVersionUID = -3820399236576088026L;

	@EJB
	private PersonDao personDao;
	
	@EJB
	private NottificationDao nottificationDao;
	
	@EJB
	private PostDao postDao; 
	
	private String username;
	private String password;
	private Integer intLoggedUserID;
	private Person loggedUser;
	
	private String postTitleVideo;
	private String postTextVideo;
	private String videoPostContent;
	
	private String vanilaURL;
	
	private Post videoPost;
	
	private Set<Person> loggedUsersFriendsSet;
	private List<Person> loggedUsersFriendsList;
	
	private List<Post> loggedUsersOwnedPosts;
	
	private Integer nottificationsCount;
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

		System.out.println("*** Loaded PostVideoOnLoggedUserWallServlet ***");

		response.setContentType("text/html; charset=UTF-8");
		request.setCharacterEncoding(response.getCharacterEncoding());

		HttpSession session = request.getSession();
		loggedUser = (Person)session.getAttribute("logovaniUser");
		
		postTitleVideo = request.getParameter("postTitleVideo");
		postTextVideo = request.getParameter("postTextVideo");
		videoPostContent = request.getParameter("videoPostContent");

		try {	
			username = loggedUser.getEmail();
			password = loggedUser.getPassword();
			intLoggedUserID = loggedUser.getId();
			
			System.out.println(username + "  " + password + "  " + intLoggedUserID);
			System.out.println(postTitleVideo + "  " + postTextVideo + "  " + videoPostContent);
		} catch (Exception e) {
			
			//e.printStackTrace();
		}
		
		if(loggedUser != null){
			try {
				// getting short URL from parameter and transforming it to the embeded URL...
				int index = videoPostContent.indexOf('&');
				if(index == -1){
					vanilaURL = videoPostContent.replace("watch?v=", "v/");
					System.out.println("vanilaURL: " + vanilaURL);
				}else if(index != -1){
					vanilaURL = videoPostContent = videoPostContent.substring(0, videoPostContent.indexOf('&'));
					vanilaURL = videoPostContent.replace("watch?v=", "v/");
					System.out.println("vanilaURL: " + vanilaURL);
				}
			} catch (Exception e) {
				
				//e.printStackTrace();
			}
			
			// creating new post...
			videoPost = new Post();
			videoPost.setTitle(postTitleVideo);
			videoPost.setText(postTextVideo);
			videoPost.setType(2);
			videoPost.setContent(vanilaURL);
			videoPost.setDate(Calendar.getInstance().getTime());

			videoPost.setPoster(loggedUser);
			videoPost.setOwner(loggedUser);

			postDao.persist(videoPost);
			
			// treba jos dodati i notifikaciju da je dati loggedd user okacio novi post...
			// adding nottification to logged user...
			Nottification nottification = new Nottification();
			nottification.setOwner(loggedUser);
			nottification.setTargetID(videoPost.getId());
			nottification.setType(2);
			nottification.setViewed(false);
			nottification.setLink("You Added New Video Clip " +
								  "<a href=\"" + videoPostContent + "\"" + " target=\"_blank\"" + ">" + 
								  "<i style=" + "\"" + "color: blue" + "\"" + ">" + videoPostContent + "</i>" + "</a>" + 
					 			  " To Your Wall.");	
			nottificationDao.persist(nottification);
			
			// sada bi trebalo dodati i po notifikaciju za svakog prijatelja logged usera da je logged user okacio novi post...
			loggedUsersFriendsSet = personDao.getFriends(intLoggedUserID);
			System.out.println("loggedUsersFriendsSet: " + loggedUsersFriendsSet.size());
			
			if(loggedUsersFriendsSet == null || loggedUsersFriendsSet.size() == 0){
				// znaci da nema prijatelja, pa idemo dalje...
				System.out.println("loggedUsersFriendsSet: " + loggedUsersFriendsSet.size());
				
			}else if(loggedUsersFriendsSet != null && loggedUsersFriendsSet.size() > 0){
				loggedUsersFriendsList = new ArrayList<Person>();
				
				Iterator<Person> iter = loggedUsersFriendsSet.iterator();
			    while(iter.hasNext()){
			    	loggedUsersFriendsList.add((Person)iter.next());
			    }
			    System.out.println("loggedUsersFriendsList: " + loggedUsersFriendsList.size());
			    
			    // sada treba za svakog prijatelja da napravimo po notifikaciju...
				for(int i = 0; i < loggedUsersFriendsList.size(); i++){
					// adding nottification to logged users friends...
					Nottification nottification2 = new Nottification();
					nottification2.setOwner(loggedUsersFriendsList.get(i));
					nottification2.setTargetID(videoPost.getId());
					nottification2.setType(2);
					nottification2.setViewed(false);
					nottification2.setLink("<a href=\"GoToSelectedPersonProfileServlet2?personID=" + loggedUser.getId() + "\"" + 
		  			  		  			   " target=\"_blank\"" + ">" + loggedUser.getFirstName() + " " + loggedUser.getLastName() + "</a>" + 
		  			  		  			   " Added New Video Clip " +
		  			  		  			   "<a href=\"" + videoPostContent + "\"" + " target=\"_blank\"" + ">" + 
		  			  		  			   "<i style=" + "\"" + "color: blue" + "\"" + ">" + videoPostContent + "</i>" + "</a>" + 
		  			  		  			   " To " + 
		  			  		  			   "<a href=\"GoToSelectedPersonProfileServlet2?personID=" + loggedUser.getId() + "\"" + 
		  			  		  			   " target=\"_blank\"" + ">" + loggedUser.getFirstName() + " " + loggedUser.getLastName() + "</a>" + "'s" + 
			 			  			   	   " Wall.");	
					nottificationDao.persist(nottification2);
				}
			}
			
			// dobavljamo 10 postova za logovanog korisnika...
			loggedUsersOwnedPosts = personDao.getFirstTenOwnedPosts(loggedUser.getId());
			System.out.println("loggedUsersOwnedPosts: " + loggedUsersOwnedPosts.size());
			
			// sada dobavljamo i notifikacije za datog ulogovanog korisnika ako ih ima...
			nottificationsCount = nottificationDao.getNottificationsCountForLoggedUser(loggedUser);
			System.out.println("nottificationsCount: " + nottificationsCount);
			
			session.setAttribute("loggedUsersOwnedPosts", loggedUsersOwnedPosts);
			session.setAttribute("nottificationsCount", nottificationsCount);
			
			response.sendRedirect("ViewWallServlet");
			return;			
		}else if(loggedUser == null){
			request.getRequestDispatcher("Welcome.jsp").forward(request, response);
			return;
		}
	}
	
	//--------------------------------------------------------------------------------------------------------------------------------------------------
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		doGet(request, response);
	}
	
	
}
